package org.waveplayer.section.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Section Item 装饰
 */
public class SectionItemDecoration extends RecyclerView.ItemDecoration {

    /**
     * Section 绘制者
     */
    private final SectionPainter sectionPainter;

    /**
     * Section 信息提供者
     */
    private final SectionProvider<?> sectionProvider;

    /**
     * 构造方法
     *
     * @param sectionProvider {@link SectionPainter}
     * @param sectionPainter  {@link SectionProvider}
     */
    public SectionItemDecoration(@NonNull SectionProvider<?> sectionProvider, @NonNull SectionPainter sectionPainter) {
        this.sectionProvider = sectionProvider;
        this.sectionPainter = sectionPainter;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        int spacing = isSameSectionAsPrevious(position) ?
                sectionPainter.getDividerHeight() : sectionPainter.getSectionBarHeight();
        outRect.set(0, spacing, 0, 0);
    }

    @Override
    public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        for (int i = 0; i < parent.getChildCount(); i++) {

            View itemView = parent.getChildAt(i);
            int position = parent.getChildAdapterPosition(itemView);
            Section section = sectionProvider.getSection(position);

            int left = itemView.getLeft();
            int right = itemView.getRight();

            if (isSameSectionAsPrevious(position)) {
                int top = itemView.getTop() - sectionPainter.getDividerHeight();
                int bottom = top + sectionPainter.getDividerHeight();
                sectionPainter.drawDivider(canvas, left, top, right, bottom);
            } else {
                int top = itemView.getTop() - sectionPainter.getSectionBarHeight();
                int bottom = top + sectionPainter.getSectionBarHeight();
                sectionPainter.drawSectionBar(canvas, left, top, right, bottom, section);
            }
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (parent.getChildCount() < 2) return;

        View itemView = parent.getChildAt(1);
        int position = parent.getChildAdapterPosition(itemView);
        Section section = sectionProvider.getSection(position);

        float translateY = 0f;
        if (!isSameSectionAsPrevious(position)) {
            section = sectionProvider.getSection(position - 1);
            if ((itemView.getTop() - sectionPainter.getSectionBarHeight() < sectionPainter.getSectionBarHeight())) {
                translateY = itemView.getTop() - (2 * sectionPainter.getSectionBarHeight());
            }
        }

        canvas.save();
        canvas.translate(0, translateY);

        int left = 0;
        int top = 0;
        int right = parent.getWidth();
        int bottom = sectionPainter.getSectionBarHeight();
        sectionPainter.drawSectionBar(canvas, left, top, right, bottom, section);

        canvas.restore();
    }

    /**
     * 判断是否和前一个数据为同一类 Section
     *
     * @param position position
     * @return true 和前一个数据为同一类 Section
     */
    private boolean isSameSectionAsPrevious(int position) {
        boolean same = false;
        if (position > 0) {
            int previous = position - 1;
            Section section = sectionProvider.getSection(position);
            if (section.equals(sectionProvider.getSection(previous)))
                same = true;
        }
        return same;
    }

}
